package com.example.udpscreen.utils;

/**
 * Created by Viper on 2016/4/22.
 */
public class Convert {


    public static String bytes2HexString(byte[] bytes,int begin,int len){
        if(bytes==null || bytes.length-(begin+len)<0)
            throw new IllegalArgumentException("this bytes must not be null or overflow total length");
        final StringBuilder hexBuilder=new StringBuilder();
        int overflow_index=begin+len;
        for(int i= begin;i<overflow_index;i++){
            if((bytes[i] & 0xff) < 0x10) //0~F 前面补零
                  hexBuilder.append('0');
            hexBuilder.append(Integer.toHexString(0xff & bytes[i]));
        }
        return hexBuilder.toString();
    }




    public static byte[] short2Bytes(short number) {
        int temp = number;
        byte[] b = new byte[2];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8; // 向右移8位
        }
        return b;
    }


    private static short bytes2Short(byte[] b) {
        short s = 0;
        short s0 = (short) (b[0] & 0xff);// 最低位
        short s1 = (short) (b[1] & 0xff);
        s1 <<= 8;
        s = (short) (s0 | s1);
        return s;
    }

    public static short bytes2UnsignedShort(byte[] b){
        return (short)(bytes2Short(b)&0xffff);
    }

    public static byte[] int2Bytes(int i) {
        byte[] bt = new byte[4];
        bt[0] = (byte) (0xff & i);
        bt[1] = (byte) ((0xff00 & i) >> 8);
        bt[2] = (byte) ((0xff0000 & i) >> 16);
        bt[3] = (byte) ((0xff000000 & i) >> 24);
        return bt;
    }

    private static int bytes2Int(byte[] bytes) {
        int num = bytes[0] & 0xFF;
        num |= ((bytes[1] << 8) & 0xFF00);
        num |= ((bytes[2] << 16) & 0xFF0000);
        num |= ((bytes[3] << 24) & 0xFF000000);
        return num;
    }


    public static byte[] long2Bytes(long number) {
        long temp = number;
        byte[] b = new byte[8];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Long(temp & 0xff).byteValue();
            // 将最低位保存在最低位
            temp = temp >> 8;
            // 向右移8位
        }
        return b;
    }


    public static long Mac2Long(byte[] b){
        long s = 0;
        long s0 = b[0] & 0xff;// 最低位
        long s1 = b[1] & 0xff;
        long s2 = b[2] & 0xff;
        long s3 = b[3] & 0xff;
        long s4 = b[4] & 0xff;// 最低位
        long s5 = b[5] & 0xff;
        long s6 =  0x00;
        long s7 =  0x00; // s0不变
        s1 <<= 8;
        s2 <<= 16;
        s3 <<= 24;
        s4 <<= 8 * 4;
        s5 <<= 8 * 5;
        s6 <<= 8 * 6;
        s7 <<= 8 * 7;
        s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
        return s;
    }
    private static long bytes2Long(byte[] b) {
        long s = 0;
        long s0 = b[0] & 0xff;// 最低位
        long s1 = b[1] & 0xff;
        long s2 = b[2] & 0xff;
        long s3 = b[3] & 0xff;
        long s4 = b[4] & 0xff;// 最低位
        long s5 = b[5] & 0xff;
        long s6 = b[6] & 0xff;
        long s7 = b[7] & 0xff; // s0不变
        s1 <<= 8;
        s2 <<= 16;
        s3 <<= 24;
        s4 <<= 8 * 4;
        s5 <<= 8 * 5;
        s6 <<= 8 * 6;
        s7 <<= 8 * 7;
        s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
        return s;
    }

    public static String bytes2MacAddressHexString(byte[] mac_address){
        if(mac_address==null || mac_address.length <1)
            throw new IllegalArgumentException("this bytes must not be null or overflow total length");
        final StringBuilder hexBuilder=new StringBuilder();
        int length=mac_address.length;
        for(int i= 0;i<length;i++){
            if((mac_address[i] & 0xff) < 0x10) //0~F 前面补零
                hexBuilder.append('0');
            hexBuilder.append(Integer.toHexString(0xff & mac_address[i]));
            hexBuilder.append(':');
        }
        hexBuilder.deleteCharAt(hexBuilder.length()-1);
        return hexBuilder.toString();
    }

    public static String byte2IPAddressString(byte[] address){
        if(address == null || address.length<1)
            throw new IllegalArgumentException("this bytes must not be null or overflow total length");
        final StringBuilder stringBuilder= new StringBuilder();
        int length = address.length;
        for (int i = 0 ; i<length;i++){
            stringBuilder.append(address[i]&0xff);
            stringBuilder.append('.');
        }
        stringBuilder.deleteCharAt(stringBuilder.length()-1);
        return stringBuilder.toString();
    }

    public static void main(String[] args){
//        byte[] mac = {(byte) 0xb8, (byte) 0x88, (byte) 0xe3,0x38, (byte) 0x8a, (byte) 0x9c};
//        long value = Convert.Mac2Long(mac);
        byte[] port = short2Bytes((short)12305);

        System.out.println(bytes2HexString(port,0,2));
    }

}
